package com.example.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Cartgoods;
import com.example.demo.mapper.CartgoodsMapper;
import com.example.demo.utils.config.MailUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@ResponseBody
@RequestMapping("/cartgoods")
public class CartgoodsController {
    @Resource
    CartgoodsMapper CartgoodsMapper;
    @PostMapping
    public Result<?> save(@RequestBody Cartgoods Cartgoods){
        CartgoodsMapper.insert(Cartgoods);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Cartgoods> wrapper = Wrappers.<Cartgoods>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Cartgoods::getName,search);
        }
            Page<Cartgoods> CartgoodsPage = CartgoodsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
            return Result.success(CartgoodsPage);

        }

    @PutMapping
    public Result<?> update(@RequestBody Cartgoods Cartgoods){
        CartgoodsMapper.updateById(Cartgoods);
        return Result.success();
//        gengxin
    }
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        CartgoodsMapper.deleteById(id);
        return Result.success();
    }

}
